package cyou.breathe.blog.folder.domain.service;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cyou.breathe.blog.api.folder.request.*;
import cyou.breathe.blog.api.folder.response.FolderCreateDirectoryResponse;
import cyou.breathe.blog.api.folder.response.FolderCreateRootResponse;
import cyou.breathe.blog.api.folder.response.FolderDeleteResponse;
import cyou.breathe.blog.api.folder.response.FolderUpdateResponse;
import cyou.breathe.blog.base.exception.RepoErrorCode;
import cyou.breathe.blog.folder.domain.entity.Folder;
import cyou.breathe.blog.folder.infrastructure.exception.FolderErrorCode;
import cyou.breathe.blog.folder.infrastructure.exception.FolderException;
import cyou.breathe.blog.folder.infrastructure.mapper.FolderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: breathe
 * @createTime: 2025-09-05
 */
@Service
public class FolderService extends ServiceImpl<FolderMapper, Folder> {

    @Autowired
    private FolderMapper folderMapper;

    @Autowired

    /**
     * 创建根目录
     * @param folderCreateRootRequest
     * @return
     */
    public FolderCreateRootResponse createRootFolder(FolderCreateRootRequest folderCreateRootRequest) {
        FolderCreateRootResponse response = new FolderCreateRootResponse();

        LambdaQueryWrapper<Folder> queryWrapper = Wrappers.lambdaQuery(Folder.class)
                .eq(Folder::getFolderName, folderCreateRootRequest.getFolderName())
                .eq(Folder::getParentId, 0);

        Folder folder = folderMapper.selectOne(queryWrapper);

        Assert.isNull(folder, () -> new FolderException(FolderErrorCode.ROOT_FOLDER_IS_EXIST));

        folder = new Folder();
        folder.createRootFolder(folderCreateRootRequest);

        if (!this.save(folder)) {
            response.setErrorCode(RepoErrorCode.INSERT_FAILED);
            return response;
        }

        response.setFolderId(folder.getId());
        response.setSuccess(true);
        return response;
    }

    /**
     * 创建子目录
     * @param request
     * @return
     */
    public FolderCreateDirectoryResponse createDirectory(FolderCreateDirectoryRequest request) {
        FolderCreateDirectoryResponse response = new FolderCreateDirectoryResponse();

        LambdaQueryWrapper<Folder> queryWrapper = Wrappers.lambdaQuery(Folder.class)
                .eq(Folder::getFolderName, request.getFolderName())
                .eq(Folder::getParentId, request.getParentId())
                .eq(Folder::getUserId, request.getUserId());
        Folder folder = folderMapper.selectOne(queryWrapper);

        Assert.isNull(folder, () -> new FolderException(FolderErrorCode.FOLDER_IS_EXIST));

        folder = new Folder();
        folder.createDirectory(request);

        if (!this.save(folder)) {
            response.setErrorCode(RepoErrorCode.INSERT_FAILED);
            return response;
        }

        response.setFolderId(folder.getId());
        response.setParentId(folder.getParentId());
        response.setSuccess(true);
        return response;
    }

    /**
     * 修改文件夹
     * @param folderUpdateRequest
     * @return
     */
    public FolderUpdateResponse updateFolder(FolderUpdateRequest folderUpdateRequest) {
        FolderUpdateResponse response = new FolderUpdateResponse();

        LambdaQueryWrapper<Folder> queryWrapper = Wrappers.lambdaQuery(Folder.class)
                .eq(Folder::getId, folderUpdateRequest.getFolderId())
                .eq(Folder::getUserId, folderUpdateRequest.getUserId());
        Folder folder = folderMapper.selectOne(queryWrapper);

        Assert.notNull(folder, () -> new FolderException(FolderErrorCode.FOLDER_NOT_EXIST));

        folder.updateFolder(folderUpdateRequest);
        if (!this.updateById(folder)) {
            response.setErrorCode(RepoErrorCode.UPDATE_FAILED);
            return response;
        }

        response.setSuccess(true);
        response.setFolderId(folder.getId());
        return response;
    }

    //查询某个目录的所有文件
    public List<Folder> queryChildByFolderId(Long folderId, Long userId) {
        LambdaQueryWrapper<Folder> queryWrapper = Wrappers.lambdaQuery(Folder.class)
                .eq(Folder::getParentId, folderId)
                .eq(Folder::getUserId, userId);
        return folderMapper.selectList(queryWrapper);
    }
    //查询目录中所有目录和文章(不递归查询)

    //查询目录中所有目录和文章(递归查询)

    //删除目录(这个得递归删除所有子目录和文章)

    public FolderDeleteResponse deleteFolder(FolderDeleteRequest request) {
        FolderDeleteResponse response = new FolderDeleteResponse();

        LambdaQueryWrapper<Folder> queryWrapper = Wrappers.lambdaQuery(Folder.class)
                .eq(Folder::getId, request.getFolderId())
                .eq(Folder::getUserId, request.getUserId());
        Folder folder = folderMapper.selectOne(queryWrapper);

        Assert.notNull(folder, () -> new FolderException(FolderErrorCode.FOLDER_NOT_EXIST));

        if (!this.removeById(folder)) {
            response.setErrorCode(RepoErrorCode.UPDATE_FAILED);
            return response;
        }

        response.setSuccess(true);
        response.setFolderId(request.getFolderId());
        return response;
    }

    public Folder getFolderById(Long folderId, Long userId) {
        LambdaQueryWrapper<Folder> queryWrapper = Wrappers.lambdaQuery(Folder.class)
                .eq(Folder::getId, folderId)
                .eq(Folder::getUserId, userId);
        return folderMapper.selectOne(queryWrapper);
    }

    public List<Folder> getFolderByParentId(Long folderId, Long userId) {
        LambdaQueryWrapper<Folder> queryWrapper = Wrappers.lambdaQuery(Folder.class)
                .eq(Folder::getParentId, folderId)
                .eq(Folder::getUserId, userId);

        return folderMapper.selectList(queryWrapper);
    }

}

